package com.chhh.cranelease.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5 {

    public static String md5(String source){

        String algorithm = "md5";

        try {
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);

            // 4.获取明文字符串对应的字节数组
            byte[] input = source.getBytes();
            // 5.执行加密
            byte[] output = messageDigest.digest(input);
            // 6.创建bigInteger
            int signum = 1;

            BigInteger bigInteger = new BigInteger(signum, output);
            // 7.按照16进制将bigInteger的值转化为字符串
            int redix = 16;
            String encoded = bigInteger.toString(redix).toUpperCase();

            return encoded;

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }

}
